home *** CD-ROM | disk | FTP | other *** search
/ Skunkware 98 / Skunkware 98.iso / src / mail / pine3.96.tar.gz / pine3.96.tar / pine3.96 / pico / fileio.c < prev    next >
C/C++ Source or Header  |  1996-06-12  |  3KB  |  146 lines

  1. #if    !defined(lint) && !defined(DOS)
  2. static char rcsid[] = "$Id: fileio.c,v 4.11 1996/06/12 17:17:06 mikes Exp $";
  3. #endif
  4. /*
  5.  * Program:    ASCII file reading routines
  6.  *
  7.  *
  8.  * Michael Seibel
  9.  * Networks and Distributed Computing
  10.  * Computing and Communications
  11.  * University of Washington
  12.  * Administration Builiding, AG-44
  13.  * Seattle, Washington, 98195, USA
  14.  * Internet: mikes@cac.washington.edu
  15.  *
  16.  * Please address all bugs and comments to "pine-bugs@cac.washington.edu"
  17.  *
  18.  *
  19.  * Pine and Pico are registered trademarks of the University of Washington.
  20.  * No commercial use of these trademarks may be made without prior written
  21.  * permission of the University of Washington.
  22.  * 
  23.  * Pine, Pico, and Pilot software and its included text are Copyright
  24.  * 1989-1996 by the University of Washington.
  25.  * 
  26.  * The full text of our legal notices is contained in the file called
  27.  * CPYRIGHT, included with this distribution.
  28.  *
  29.  */
  30. /*
  31.  * The routines in this file read and write ASCII files from the disk. All of
  32.  * the knowledge about files are here. A better message writing scheme should
  33.  * be used.
  34.  */
  35. #include        <stdio.h>
  36. #include    <errno.h>
  37. #include    "osdep.h"
  38. #include    "pico.h"
  39. #include    "estruct.h"
  40. #include        "edef.h"
  41.  
  42.  
  43. #if    defined(bsd) || defined(lnx)
  44. extern int errno;
  45. #endif
  46.  
  47.  
  48. FILE    *ffp;                           /* File pointer, all functions. */
  49.  
  50. /*
  51.  * Open a file for reading.
  52.  */
  53. ffropen(fn)
  54.   char    *fn;
  55. {
  56.     int status;
  57.  
  58.     if ((status=fexist(fn, "r", NULL)) == FIOSUC)
  59.       if ((ffp=fopen(fn, "r")) == NULL)
  60.     status = FIOFNF;
  61.  
  62.     return (status);
  63. }
  64.  
  65.  
  66. /*
  67.  * Write a line to the already opened file. The "buf" points to the buffer,
  68.  * and the "nbuf" is its length, less the free newline. Return the status.
  69.  * Check only at the newline.
  70.  */
  71. ffputline(buf, nbuf)
  72.     CELL  buf[];
  73. {
  74.     register int    i;
  75.  
  76.     for (i = 0; i < nbuf; ++i)
  77.        if(fputc(buf[i].c&0xFF, ffp) == EOF)
  78.      break;
  79.  
  80.    if(i == nbuf)
  81.       fputc('\n', ffp);
  82.  
  83.     if (ferror(ffp)) {
  84.         emlwrite("\007Write error: %s", errstr(errno));
  85.     sleep(5);
  86.         return (FIOERR);
  87.     }
  88.  
  89.     return (FIOSUC);
  90. }
  91.  
  92.  
  93.  
  94. /*
  95.  * Read a line from a file, and store the bytes in the supplied buffer. The
  96.  * "nbuf" is the length of the buffer. Complain about long lines and lines
  97.  * at the end of the file that don't have a newline present. Check for I/O
  98.  * errors too. Return status.
  99.  */
  100. ffgetline(buf, nbuf)
  101.   register char   buf[];
  102. {
  103.     register int    c;
  104.     register int    i;
  105.  
  106.     i = 0;
  107.  
  108.     while ((c = fgetc(ffp)) != EOF && c != '\n') {
  109.     /*
  110.      * Don't blat the CR should the newline be CRLF and we're
  111.      * running on a unix system.  NOTE: this takes care of itself
  112.      * under DOS since the non-binary open turns newlines into '\n'.
  113.      */
  114.     if(c == '\r'){
  115.         if((c = fgetc(ffp)) == EOF || c == '\n')
  116.           break;
  117.  
  118.         if (i < nbuf-2)        /* Bare CR. Insert it and go on... */
  119.           buf[i++] = '\r';        /* else, we're up a creek */
  120.     }
  121.  
  122.         if (i >= nbuf-2) {
  123.         buf[nbuf - 2] = c;    /* store last char read */
  124.         buf[nbuf - 1] = 0;    /* and terminate it */
  125.             emlwrite("File has long line");
  126.             return (FIOLNG);
  127.         }
  128.         buf[i++] = c;
  129.     }
  130.  
  131.     if (c == EOF) {
  132.         if (ferror(ffp)) {
  133.             emlwrite("File read error");
  134.             return (FIOERR);
  135.         }
  136.  
  137.         if (i != 0)
  138.       emlwrite("File doesn't end with newline.  Adding one.", NULL);
  139.     else
  140.       return (FIOEOF);
  141.     }
  142.  
  143.     buf[i] = 0;
  144.     return (FIOSUC);
  145. }
  146.